package com.kellyfeng.spittr.rest;

import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.kellyfeng.spittr.controller.SpittleForm;
import com.kellyfeng.spittr.domain.Spittle;
import com.kellyfeng.spittr.repository.*;

@RestController
@RequestMapping("/spittlesrest")
public class SpittleRest {
	private SpittleRepository spittleRepository;
	
	@Autowired
	public SpittleRest(SpittleRepository spittleRepository) {
		this.spittleRepository = spittleRepository;
	}
	@RequestMapping(method=RequestMethod.GET)
	public List<Spittle> spittles(@SortDefault(sort = "id", direction = Direction.DESC) 
	Sort sort) {
		return spittleRepository.findAll(sort);
	}
	@RequestMapping(method=RequestMethod.POST, consumes="application/json")
	public Spittle saveSpittle(@RequestBody Spittle spittle){
		return spittleRepository.save(spittle);
	}
	@RequestMapping(value = "/{spittleId}", method = RequestMethod.GET)
	public Spittle spittle(@PathVariable("spittleId") long spittleId) {
		return spittleRepository.findOne(spittleId);
	}
}
